
var com = new CommonUtil();
$(function(){
	var Examine = {
		init:function () {
			this.bindClick();
			this.loadData();
			this.loadData({"day":"all"});
		},
		orderNo:'',
		bindClick:function(){
			var e = this;
			$(".fter ul li").each(function(){
				$(this).bind("click",function () {
					var name = $(this).find("b").attr("textname")
					var data = {"day":name};
					e.loadData(data);
					$(this).find('b').css('color', '#2eafbb');
					$(this).siblings().find('b').css('color', 'black');
				});
			});

		},
		callTotal:function(data){
			if(data!=null){
				var $fter = $(".fter");
				var num=0;
				$.each(data[0],function (i, val) {
					val ="("+val+")";
					$fter.find("li").eq(num).find("span").text(val);
					num++;
				});
			}
		},
		callHtml:function(data){
			var self = this;
			if(data!=null){
				var $detailed = $(".detailed");
				$detailed.empty();
				$.each(data,function (ind,d) {
					var goodsTypes = {"1":"医疗备品","2":"备品","0":"其他"};
					var html ='<div class="tysp"><p> 订单号:  <span>'+d.orderNo+'</span><input class="orderId" type="hidden" value="'+d.id+'"><input class="orderNo" type="hidden" value="'+d.orderNo+'"></p>';
						html +='<p>寄件人:  <span>'+d.sendCompany+'</span> </p>';
						html +='<p>收件人:  <span>'+d.receiveAddress+'</span> </p>';
						html +='<p>物品信息:<span>'+goodsTypes[d.goodsType]+'</span>';
						html +=	'<span>'+(d.goodsNumber==null?"":d.goodsNumber)+'件'+(d.weight==null?"":d.weight)+'kg</span></p>';
						html +='<p>备注信息:<span>'+(d.remark==null?"":d.remark)+'</span></p>';
						html +='<p>下单时间:<span>'+d.createDate+'</span></p>';
						html +='<p>取件时间:<span>'+(d.receiveDate==null?'':d.receiveDate)+'</span></p>';
						var curnTime = new Date().getTime()/1000;
						var oldTime = Date.parse(com.formatDate(d.createDate))/1000;
						html +="<p style='text-align: right;margin-right: 4%;'>"
						if(curnTime -oldTime <= cancelTime){
							html +='<button type="button" class="cancel layui-btn layui-btn-primary">取消</button>';
						} else if(curnTime -oldTime <= editTime){
							html +='<button type="button" class="edit layui-btn layui-btn-primary">编辑</button>';
						} else {
							var sysUserId = $("#sysUserId").val();
							if(!com.isNull(sysUserId)){
								html +='<button type="button" class="re_query layui-btn layui-btn-primary">查询</button>';
								html +='<button type="button" class="re_save layui-btn layui-btn-primary">录入</button>';
							}
						}
						html +="</p>";
						// html +="</p>";
						html +="</div>";
						$detailed.append(html);
				});
				// 取消
				$(".cancel").each(function() {
					$(this).click(function () {
						Examine.cancel($(this).parents(".tysp").find(".orderId").val(), $(this));
					});
				});
				// 编辑
				$(".edit").each(function() {
					$(this).click(function () {
						Examine.edit($(this).parents(".tysp").find(".orderId").val(), $(this));
					});
				});
				// 查询
				$(".re_query").each(function() {
					$(this).click(function () {
						Examine.qeryReceipt($(this).parents(".tysp").find(".orderId").val(), $(this));
					});
				});
				// 录入
				$(".re_save").each(function() {
					$(this).click(function () {
						var sysUserId = $("#sysUserId").val();
						Examine.orderNo = $(this).parents(".tysp").find(".orderNo").val();
						console.log("后台登录的用户是："+sysUserId);
						if(com.isNull(sysUserId)){
							var html = '';
							html+='<div class="layui-form-item">';
							html+='		<label class="layui-form-label">用户</label>';
							html+='		<div class="layui-input-block">';
							html+='			<input type="text" name="username" id="username" lay-verify="title" autocomplete="off" placeholder="请输用户名" class="layui-input">';
							html+='		</div>';
							html+='	</div>';
							html+='	<div class="layui-form-item">';
							html+='		<label class="layui-form-label">密码</label>';
							html+='		<div class="layui-input-block">';
							html+='			<input type="password" name="password" id="password" placeholder="请输入密码" autocomplete="off" class="layui-input">';
							html+='		</div>';
							html+='	</div>';
							// var $loginModal = $("#loginModal");
							// $loginModal.append(html);
							layer.open({
								content: html
								, btn: ['确定']
								,yes: function(index) {
									var username = $("#username").val();
									var password = $("#password").val();
									layer.close(index);
									if(com.isNull(username) || com.isNull(password)){
										layer.open({
											content: "账号密码不能为空"
											,btn: '确定'
										});
									} else {
										// console.log("username:"+username+",password:"+password);
										Examine.ajaxRequst("addLogin?username="+username+"&password="+password,"get", {},Examine.goToAddInfo,null);
									}
								}
							});
						} else {
							Examine.goToAddInfo();
						}
					});
				});
			}
		},
		goToAddInfo(){
			window.location.href = "toAddInfo?orderNo="+Examine.orderNo;
		},
		cancel:function(id,obj){
			this.ajaxRequst("cancelOrder","get", {id:id},this.isReload,obj);
		},
		edit:function(id){
			window.location.href = "sendIndex?id="+id;
		},
		isReload:function(obj){
			//obj.parents(".tysp").remove();
			window.location.reload();
		},
		toTime:function(date){
			return Date.parse(date)/1000;
		},
		qeryReceipt:function(id,obj){
			this.ajaxRequst("qeryOrderReceipt?orderId="+id,"get", {id:id},this.loadReceipt,obj);
		},
		loadReceipt:function (obj,data){
			var $receipt= $(obj);
			var htmls = '';
			var infoList = data.infosList;
			if(data!=null && infoList.length>0){
				var $tysp = $receipt.parents(".tysp");
				htmls = '<div class="container"><div class="timeline">';
				htmls += '<div class="event">';
				htmls += '<div class="status">托运单号:&nbsp;&nbsp;'+data.oddNumber+'</div>';
				htmls += '<div class="location">寄件地:&nbsp;&nbsp;'+data.shippingAddr+'</div>';
				htmls += '<div class="status">寄件单位联系人:&nbsp;&nbsp;'+data.unitName+'</div>';
				htmls += '<div class="status">寄件单位联系人:&nbsp;&nbsp;'+data.unitPhone+'</div>';
				htmls += '<div class="status">目的地:&nbsp;&nbsp;'+data.dest+'</div>';
				htmls += '<div class="status">收件人:&nbsp;&nbsp;'+data.receiver+'</div>';
				htmls += '<div class="location">收件地址:&nbsp;&nbsp;'+data.receiverAddr+'</div>';
				htmls += '<div class="location">收货人电话:&nbsp;&nbsp;'+data.receiverPhone+'</div>';
				htmls += '<div class="location">货物名称:&nbsp;&nbsp;'+data.goodsName+'</div>';
				htmls += '<div class="location">件数:&nbsp;&nbsp;'+data.numbered+'</div>';
				htmls += '<div class="location">重量:&nbsp;&nbsp;'+data.weight+'</div>';
				htmls += '<div class="time">预计到达日期:&nbsp;&nbsp;'+data.updateDate+'</div>';
				htmls += '</div>';
				$.each(infoList,function (ind,d) {
					htmls += '<div class="event">';
					htmls += '<div class="status">托运单号:&nbsp;&nbsp;'+d.status+'</div>';
					htmls += '<div class="time">寄件地:&nbsp;&nbsp;'+d.tranTime+'</div>';
					htmls += '<div class="location">寄件地:&nbsp;&nbsp;'+d.addr+'</div>';
					htmls += '</div>';
				});
				htmls += '</div></div>';
				// $tysp.append(htmls);
			} else {
				htmls ='暂无数据';
			}
			layer.open({
				content: htmls
				,btn: '确定'
			});
		},
		loadData: function (data) {
			if (data != null && data != undefined) {
				this.ajaxRequst("getListByDay", "get", data, this.callHtml);
			} else {
				this.ajaxRequst("getTotal","get", {},this.callTotal);
			}
		},
		/**
		 * 发起ajax请求
		 */
		ajaxRequst:function(url,type,data,call,obj){
			var o = this;
			if(!url || !data){
				return;
			}
			$.ajax({
				url:url,
				type: type,
				dataType: "json",
				data: data,
//                async: false,
//                contentType:'application/json;charset=UTF-8',
				success: function(data) {
//                	var data = eval("("+data+")");//转换为json对象
					if(data.status=="200" ) {
						if ($.isFunction(call)) {
							call(obj == undefined ? data.list : obj);
						}
					} else if(data.code=="200"){
						if ($.isFunction(call)) {
							call(obj == undefined ? null: obj, data.data.receiptData );
						}
					} else {
						//信息框
						layer.open({
							content: data.message
							,btn: '确定'
						});
					}
				},
				error: function() {
					//信息框
					layer.open({
						content: '数据出现问题,请重试'
						,btn: '确定'
					});
				}
			});
//        	return false; // 阻止表单自动提交事件
		}
	}
	Examine.init();
})
